package com.wisnews.mapper.sr;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.wisnews.pojo.sr.entity.News;
import com.wisnews.pojo.sr.vo.NewsStatistics;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author yuyd
 * @since 2020-12-25
 */
@Mapper
public interface NewsMapper extends BaseMapper<News> {
    /**
     * @description:
     *   用数据库函数 substring_index 截取文章前两个 p 标签 用于简略信息展示 解决前台bug
     *   str_to_date(a.news_announce_time,'%Y-%m-%d %H:%i:%s') 加了时间处理函数，用于排序
     * @param:
     * @return:
     * @author yuyd
     * @date: 2021/5/2 23:06
     */
    @Select("SELECT a.news_id,a.news_title ,str_to_date(a.news_announce_time,'%Y-%m-%d %H:%i:%s') as news_announce_time,a.news_from_url ,a.news_comments,\n" +
            "            CONCAT(substring_index(a.news_content,'</p>',2),'</p>') as news_content,a.news_media,a.news_read_quantity,a.modification_time, a.create_time ," +
            " REPLACE(a.news_type,'网易首页, ','') as news_type,a.news_home_imgurl,a.news_state \n" +
            "            from t_sr_news a ${ew.customSqlSegment}")
    IPage<News> getNewsInfo4Page(IPage toPage,@Param(Constants.WRAPPER) Wrapper<News> wrapper);

    /**
     * @description: 获取所有的原文地址
     * @param:
     * @return: url
     * @author yuyd
     * @date: 2021/5/2 23:07
     */
    @Select("select a.news_from_url from t_sr_news a")
    List<String> getAllPageUrl();



    /**
     * @description: 根据新闻id，批量更新 新闻状态
     * @param:
     * @return:
     * @author yuyd
     * @date: 2021/5/2 22:31
     */
    @Update("<script> " +
            "update t_sr_news set news_state = #{sta} " +
            "<where>\n" +
            " <foreach item=\"item\" index=\"index\" collection=\"ids\"\n" +
            "      separator=\" or \" >\n" +
            "      news_id = #{item} \n" +
            "  </foreach>  " +
            "</where>\n" +
            "</script> ")
    void bathUpdateState(@Param("ids") List<String> ids,@Param("sta") String state);

    @Select("SELECT COUNT(*) AS type_number ,SUBSTRING(a.news_type,8,4) AS news_type FROM t_sr_news a GROUP BY SUBSTRING(a.news_type,8,4);")
    List<NewsStatistics> getNewsCountInfo();


    @Select(" select * from t_sr_news a where news_operator_id = #{userId} ")
    IPage<News> getNewsListForUserId(IPage toPage,@Param("userId") String operatorId);
}
